home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_179 / unixutil / head.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  95 lines

  1. /* head.c - print the first few lines in a file.  If count is specified,*
  2.  *    that many lines are printed instead of the default value of    *
  3.  *    ten lines.  If no files are specified, head reads standard    *
  4.  *    input.                                *
  5.  *                                    *
  6.  * head [-<count>] [<file> ...]                     *
  7.  *                                    *
  8.  * head (C) 1988 by Gary L. Brant                    *
  9.  *                                    *
  10.  * :ts=8                                */
  11.  
  12. #include <stdio.h>
  13. #define   MAXLINE   1000
  14.  
  15. void fputs ();
  16. int head = 0;
  17.  
  18. main (argc, argv)    /* list 1st n lines of a file */
  19. int  argc;
  20. char *argv[];
  21. {
  22.    FILE *input, *fopen ();
  23.    void fclose ();
  24.    int default_lines = 10;    /* default number of lines to list */
  25.    int i = 0, j, maxlines;
  26.    char ch;
  27.  
  28.    maxlines = default_lines;
  29.    while (++i < argc) {
  30.       if (argv [i][0] == '-') {     /* remember to bump i in loop */
  31.      maxlines = 0;    /* remember to set maxlines to 0 1st */
  32.      for (j = 1; (ch = argv[i][j]) != '\0'; j++)
  33.         if (ch >= '0' && ch <= '9') {
  34.            maxlines *= 10;
  35.            maxlines += ch - '0';
  36.         } else {
  37.            maxlines = default_lines;
  38.            break;
  39.         }
  40.      default_lines = maxlines;
  41.       } else {
  42.      ++head;
  43.      if ((input = fopen (argv[i], "r")) == NULL) {
  44.         fputs ("head: can't open ", stderr);
  45.         fputs (argv[i], stderr);
  46.         putc ('\n', stderr);
  47.         break;
  48.      } else {
  49.         list (input, argv[i], maxlines);
  50.         fclose (input);
  51.      }
  52.       }
  53.    }
  54.    if (!head)
  55.       list (stdin, "", maxlines);
  56. }
  57.  
  58.  
  59. /* list head of a file */
  60.  
  61. list (fp, fn, maxlines)
  62. FILE *fp;
  63. char *fn;
  64. int maxlines;
  65. {
  66.    int n;
  67.    int i = 0;
  68.    char line[MAXLINE];
  69.    char *fgets ();
  70.  
  71.    while ((i < maxlines) && (fgets (line, MAXLINE, fp)) != NULL) {
  72.       if (i++ == 0)
  73.      heading (fn);
  74.       fputs (line, stdout);
  75.    }
  76. }
  77.  
  78.  
  79. /* heading - print a short heading identifying file */
  80.  
  81. heading (filename)
  82. char *filename;
  83. {
  84.    if (head) {
  85.       fputs ("==> ", stdout);
  86.       fputs (filename, stdout);
  87.       fputs (" <==\n", stdout);
  88.    }
  89. }
  90.  
  91.  
  92. _wb_parse ()
  93. {
  94. }
  95.